using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api.Mobile
{
    /// <summary>
    /// 隐患_排查计划主表(隐患排查任务)
    ///</summary>
    [ApiExplorerSettings(GroupName = "Mobile")]//swagger分类标签
    [Route("api/mobile/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class YH_ScreeningPlanController : CoreApiController
    {
        private readonly YH_ScreeningPlanService _service;
        /// <summary>
        /// YH_ScreeningPlan
        ///</summary>
        public YH_ScreeningPlanController(YH_ScreeningPlanService service)
        {
            _service = service;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(YH_ScreeningPlanController) + "_" + nameof(YH_ScreeningPlanController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult> GetList(PagingModel<YH_ScreeningPlanDto> pagingQueryModel)
        {
            IPageModel<YH_ScreeningPlanDto>
                result = await _service.GetList(pagingQueryModel);
            PageModelObj retObj = new PageModelObj();
            retObj.PageData = result.PageData?.Select(a => new
            {
                a.Objective,//排查目的
                a.PersonInCharge,//负责人Id
                a.PersonInChargeText,//负责人
                a.BeginTime,//计划开始时间
                PlanState = new Random().Next(0, 2).ToString(),//状态：无字段，逻辑控制。(0未开始 1进行中 2已完成)
                YH_ScreeningPlanProject = a.YH_ScreeningPlanProject.Select(b => new
                {
                    b.Region,//区域ID
                    RegionText = b.SysArea.Name,//区域
                    a.BeginTime,//任务开始时间
                    a.PersonInChargeText,////负责人
                    ScreeningPlanProject = new Random().Next(0, 2).ToString(),////状态：无字段，逻辑控制。(0未开始 1进行中 2已完成)
                    //隐患排查任务详情
                    ScreeningPlanProjectDetails = new
                    {
                        b.Project,//排查项目
                        a.PersonInCharge,//负责人Id
                        a.PersonInChargeText,//负责人
                        a.Unit,//排查单位
                        a.Objective,//排查目的
                        YH_ScreeningPlanUser = a.YH_ScreeningPlanUser.Select(d => new { d.Id, d.UserId, d.UserText }),//参与排查人员
                        a.Requirements,//排查要求
                        a.Content,//排查内容,
                        a.StandardType,//排查标准类型 年月日
                        a.InvestigationPost,//排查岗位
                        b.Region,//区域ID
                        RegionText = b.SysArea.Name,//区域
                        b.Remark,//备注
                        //隐患_排查计划项目明细
                        YH_ScreeningPlanProDetail = b.YH_ScreeningPlanProDetail.Select(d => new
                        {
                            d.Id,
                            d.ScreeningPlanProjectId,//隐患_基础管理类排查库ID
                            d.StandardContent,//排查标准内容
                            d.RiskLevel,//隐患等级ID
                            d.RiskLevelText
                        })
                    }
                })
                //a.Unit,//排查单位
                //a.Requirements,//排查要求
                //a.Content,//排查内容
                //a.StandardType,//排查标准类型
                //a.InvestigationPost,//排查岗位
                //a.Remark,//备注
                //a.IsNotRemind,//是否提醒
                //a.MoveUp,//提前提醒天数
                //a.Type,//提醒类别(年月)
                //a.IsProceed,//计划是否正在进行(进行中/关闭)
                //a.Status,//状态(保存/提交)
                //a.CreateFactory,//隶属工厂
                //a.CreateDept,//
                //a.UpdateDept,//
            }
            ).ToList();
            retObj.RowCount = result.RowCount;
            return this.JsonSuccess(retObj);
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(YH_ScreeningPlanController) + "_" + nameof(YH_ScreeningPlanController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult> SaveAsync(YH_ScreeningPlanDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(YH_ScreeningPlanController) + "_" + nameof(YH_ScreeningPlanController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult> Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            var retObj = new
            {
                model.Unit,//排查单位
                model.Objective,//排查目的
                model.PersonInCharge,//负责人
                model.Requirements,//排查要求
                model.Content,//排查内容
                model.StandardType,//排查标准类型
                model.InvestigationPost,//排查岗位
                model.Remark,//备注
                model.IsNotRemind,//是否提醒
                model.MoveUp,//提前提醒天数
                model.Type,//提醒类别(年月)
                model.BeginTime,//计划开始时间
                model.IsProceed,//计划是否正在进行(进行中/关闭)
                model.Status,//状态(保存/提交)
                model.CreateFactory,//隶属工厂
                model.CreateDept,//
                model.UpdateDept,//
            };
            return this.JsonSuccess(retObj);
        }


        #endregion


    }
}
